package com.example.task1.ui.AllService.pet;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.example.task1.util.Utils;

import java.io.File;
import java.util.HashMap;

import retrofit2.Call;

public class PetCreateActivity extends AppCompatActivity {

    private int mId;
    private EditText etContent;
    private ImageView image;
    private Button btnSave;
    private String mFileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_create);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("创建咨询");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        mId = getIntent().getIntExtra("id", 0);
        initView();
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("doctorId", mId);
                map.put("question", etContent.getText().toString());
                if (TextUtils.isEmpty(mFileName)) {
                    map.put("imageUrls", mFileName);
                }
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/pet-hospital/inquiry", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show(data.getMsg());
                        finish();
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.opImage(PetCreateActivity.this);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 2) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 读取存储权限已被授予，可以执行打开相册的操作
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 1);
            } else {
                // 用户拒绝了权限请求，您可以向用户解释为什么需要这个权限
                MyApplication.show("访问相册需要授权");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
            Uri uri = data.getData();
            Glide.with(this).load(uri).into(image);
            File file = Utils.getFile(this, uri);
            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/common/upload", Api.getFileBody(file));
            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                @Override
                public void success(ApiResp data) {
                    mFileName = data.getFileName();
                }

                @Override
                public void error(String msg) {

                }
            });
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        etContent = findViewById(R.id.et_content);
        image = findViewById(R.id.image);
        btnSave = findViewById(R.id.btn_save);
    }
}